﻿using maxiaopeng.Help;
using maxiaopeng.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace maxiaopeng.Controllers
{
    public class HomeController : ApiController
    {
        /// <summary>
        /// 修改密码处理
        /// </summary>
        /// <param name="pass"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult password([FromBody] pass pa)
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                var user = db.Users.FirstOrDefault(p => p.Id == pa.Id);
                if (user != null)
                {
                    if (user.Password == pa.password)
                    {
                        user.Password = pa.newpassword;
                        int n = db.SaveChanges();
                        if (n > 0)
                        {
                            return Json(new { code = 0, msg = "密码修改成功！" });
                        }
                        else
                        {
                            return Json(new { code = -1, msg = "密码修改失败！" });
                        }
                    }
                    else
                    {
                        return Json(new { code = -1, msg = "原始密码输入不正确！" });
                    }
                }
                else
                {
                    return Json(new { code = -1, msg = "用户不存在！" });
                }
            }
        }
        // GET: api/Home
        /// <summary>
        /// 获取穿梭框数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetRoles(int Id)
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                var list = db.UserRoles.Where(t => t.UserId == Id).ToList();
                var Roleslist = db.Roles.Select(t => new { key = t.Id, label = t.Name }).ToList();
                if (list.Count > 0)
                {
                    var ss = "";
                    foreach (var item in list)
                    {
                        ss += Convert.ToString(item.RoleId) + ",";
                    }

                    ss = ss.Substring(0, ss.Length - 1);
                    var mo = ss.Split(',').Select(x => int.Parse(x)).ToList();

                    return Json(new { code = 0, data = Roleslist, valus = mo, userId = Id });
                }
                else
                {
                    return Json(new { code = -1, data = Roleslist, valus = "", userId = Id });
                }

            }
        }
        [HttpGet]
        public IHttpActionResult GetUsersRolesAdd(int userId, string movedKey)
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                var mo = movedKey.Split(',').Select(x => int.Parse(x)).ToList();
                for (int i = 0; i < mo.Count; i++)
                {
                    UserRoles ur = new UserRoles() { UserId = userId, RoleId = mo[i] };
                    db.UserRoles.Add(ur);
                }
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = 0, msg = "添加成功" });
                }
                else
                {
                    return Json(new { code = -1, msg = "添加失败" });
                }
            }
        }
        [HttpGet]
        public IHttpActionResult GetUsersRolesDel(int userId, string movedKey)
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                var mo = movedKey.Split(',').Select(x => int.Parse(x)).ToList();
                foreach (var item in mo)
                {
                    var ss = db.UserRoles.FirstOrDefault(t => t.RoleId == item && t.UserId == userId);
                    db.UserRoles.Remove(ss);
                }
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = 0, msg = "删除成功" });
                }
                else
                {
                    return Json(new { code = -1, msg = "删除失败" });
                }
            }
        }
        public IHttpActionResult resetuser([FromBody] Users us)
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                var ddd = db.Users.FirstOrDefault(p => p.Id == us.Id);
                if (ddd != null)
                {
                    ddd.Password = "123456";
                    int n = db.SaveChanges();
                    if (n > 0)
                    {
                        return Json(new { code = 0, msg = "重置成功！" });
                    }
                    else
                    {
                        return Json(new { code = -1, msg = "重置失败！" });
                    }
                }
                else
                {
                    return Json(new { code = -1, msg = "错误操作！" });
                }
            }
        }
        [HttpPost]
        public IHttpActionResult Delete([FromBody]Users user)
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                var list = db.Users.FirstOrDefault(p => p.Id == user.Id);
                db.Users.Remove(list);
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = 0, msg = "删除成功！" });
                }
                else
                {
                    return Json(new { code = -1, msg = "删除失败！" });
                }
            }
        }
        [HttpPost]
        public IHttpActionResult Edit([FromBody]Users add)
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                var list = db.Users.FirstOrDefault(p => p.Id == add.Id);
                list.LoginName = add.LoginName;
                list.ProtectEmail = add.ProtectEmail;
                list.ProtectTel = add.ProtectTel;
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = 0, msg = "修改成功！" });
                }
                else
                {
                    return Json(new { code = -1, msg = "修改失败！" });
                }
            }
        }
        [HttpPost]
        public IHttpActionResult Idlockout([FromBody]Users ff)
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                Users list = db.Users.FirstOrDefault(x => x.Id == ff.Id);
                if (list.IsLockout == 0)
                {
                    list.IsLockout = 1;
                    list.LockTime = DateTime.Now;
                    int n = db.SaveChanges();
                    if (n > 0)
                    {
                        return Json(new { code = 0, msg = "锁定成功" });
                    }
                    else
                    {
                        return Json(new { code = 0, msg = "锁定失败" });
                    }
                }
                else
                {
                    list.IsLockout = 0;
                    int n = db.SaveChanges();
                    if (n > 0)
                    {
                        return Json(new { code = 0, msg = "解冻成功" });
                    }
                    else
                    {
                        return Json(new { code = 0, msg = "解冻失败" });
                    }
                }
            }
        }
        [HttpPost]
        public IHttpActionResult addUsers([FromBody]Users add)
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                db.Users.Add(add);
                add.IsLockout = 0;
                add.LastLoginTime = DateTime.Now;
                add.PsdWrongTime = 0;
                add.LockTime = DateTime.Now;
                add.CreateTime = DateTime.Now;
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = 0, msg = "添加成功！" });
                }
                else
                {
                    return Json(new { code = -1, msg = "添加失败！" });
                }
            }
        }
        [HttpGet]
        public IHttpActionResult Users(int page, int limit, string LoginName, string IsLockout)
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                var dataList = db.Users.AsEnumerable();
                var count = dataList.Count();
                if (!string.IsNullOrEmpty(LoginName))
                {
                    dataList = dataList.Where(p => p.LoginName.Contains(LoginName));
                }
                if (!string.IsNullOrEmpty(IsLockout))
                {
                    dataList = dataList.Where(p => p.IsLockout==Convert.ToInt32(IsLockout));
                }

                //筛选页面所有符合条件的数据
                var list = dataList.Select(p => new
                {
                    Id = p.Id,
                    LoginName = p.LoginName,
                    Password = p.Password,
                    IsLockout = p.IsLockout,
                    LastLoginTime = p.LastLoginTime.ToString(),
                    CreateTime = p.CreateTime.ToString(),
                    PsdWrongTime = p.PsdWrongTime.ToString(),
                    LockTime = p.LockTime,
                    ProtectEMail = p.ProtectEmail,
                    ProtectTel = p.ProtectTel,
                });
                return Json(new { count = count, data = list.OrderByDescending(p => p.Id).Skip((page - 1) * 10).Take(limit).ToList() });

            }
        }
    }
}
